package com.esiag.isidis.receiver;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import MainTest.Creator;

import com.esiag.isidis.beans.ConnectionInfo;
import com.esiag.isidis.beans.DefaultConnectionInfo;
import com.esiag.isidis.beans.DefaultTrameInfo;
import com.esiag.isidis.beans.Trame;
import com.esiag.isidis.beans.TrameInfo;
import com.esiag.isidis.interfaces.IReceiver;

public class UDPReceiver implements IReceiver {
	private ConnectionInfo connInfo;
	private Trame trame;
	
	public UDPReceiver() {
		super();
	}
	
	public boolean startReceiver() {
		TrameInfo trameInfo = DefaultTrameInfo.getDefaultTrameInfo();
		connInfo = DefaultConnectionInfo.getDefaultConnectionInfo();
		trame = new Trame();
		trame.setTrameInfo(trameInfo);
		try{
			 DatagramSocket serverSocket = new DatagramSocket(connInfo.getPort());
			 
	         System.out.println("[Receiver] Receveur en marche ...");
	            while(true)
	            {
	               System.out.println("[Receiver] en attente de Message ...");
	               
	               DatagramPacket receivePacket = new DatagramPacket(trame.getMessage(), trame.getMessage().length);
	               serverSocket.receive(receivePacket);
	               
	               System.out.println("[Receiver] Message recu { " + receivePacket.getData()[0] + " , " + receivePacket.getData()[1] + " }" );

	       		   Creator creator = new Creator();
	       		   creator.setTrame(receivePacket.getData());
	            }
			}catch(Exception ex){
				System.out.println();
			}
		
		return true;
	
	}

	

	
	public void setTrame(Trame trame) {
		this.trame = trame;
	}
	public Trame getTrame() {
		return trame;
	}

}
